package com.taotao.sso.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.converter.json.MappingJacksonValue;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.taotao.common.pojo.TaotaoResult;
import com.taotao.common.utils.ExceptionUtil;
import com.taotao.pojo.TbUser;
import com.taotao.sso.service.RegisterService;

/**
 * 注册时数据校验
 * <p>
 * Title:RegisterController
 * </p>
 * <p>
 * Description:
 * </p>
 * 
 * @author JSJ
 * @date 2017年10月12日下午9:00:12
 */
@Controller
@RequestMapping("/user")
public class RegisterController {
	@Autowired
	private RegisterService registerService;

	/**
	 * 注册单一数据校验(username,phone,email)
	 * 
	 * @param param
	 * @param type
	 * @param callback
	 * @return
	 * @author: JSJ
	 */
	@ResponseBody
	@RequestMapping("/check/{param}/{type}")
	public Object checkData(@PathVariable String param,
			@PathVariable Integer type, String callback) {
		try {
			TaotaoResult result = registerService.checkData(param, type);
			// 判断调用是否存在回调函数,存在则将数据转换为jsonp格式
			if (StringUtils.isNotEmpty(callback)) {
				// 转换为jsonp传输方式
				MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(
						result);
				mappingJacksonValue.setJsonpFunction(callback);
				return mappingJacksonValue;
			}
			// 不存在回调,直接返回
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e));
		}
	}
	/**
	 * 注册用户服务
	 * @param tbUser
	 * @return
	 * @author: JSJ
	 */
	@ResponseBody
	@RequestMapping(value="/register",method=RequestMethod.POST)
	public TaotaoResult register(TbUser tbUser){
		try {
			TaotaoResult result = registerService.register(tbUser);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return TaotaoResult.build(500,ExceptionUtil.getStackTrace(e));
		}
	}
	
	
	
}
